package com.example.photography.adapter.home_recommed;

import android.content.Context;
import android.view.View;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import com.alibaba.fastjson.JSONObject;
import com.bumptech.glide.Glide;
import com.bumptech.glide.load.resource.bitmap.RoundedCorners;
import com.bumptech.glide.request.RequestOptions;
import com.example.photography.R;
import com.example.photography.adapter.LabelAdapter;
import com.example.photography.adapter.MyBaseAdapter;
import com.example.photography.bean.Home_RecommendBean;
import com.example.photography.bean.UrlBean;
import com.example.photography.ui.CircleImageView;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;

public abstract class Item_productAdapter extends MyBaseAdapter {

    private List<UrlBean> urlBeans;

    public Item_productAdapter(List list, Context context) {
        super(list, context);
    }

    @Override
    public View myGetView(int position, View convertView) {
        ViewHolder viewHolder = null;
        if (convertView == null) {
            convertView = View.inflate(mContext, R.layout.item_home_product, null);
            viewHolder = new ViewHolder(convertView);
            convertView.setTag(viewHolder);
        } else {
            viewHolder = (ViewHolder) convertView.getTag();
        }
        Home_RecommendBean.ProductListBean productListBean = (Home_RecommendBean.ProductListBean) list.get(position);
        Glide.with(mContext)
                .load(productListBean.getHead_url())
                .thumbnail(Glide.with(mContext).load(R.drawable.loading))
                .apply(RequestOptions.bitmapTransform(new RoundedCorners(30)))
                .into(viewHolder.ivHead);

        viewHolder.tvName.setText(productListBean.getName());
        urlBeans = JSONObject.parseArray(productListBean.getProducts(), UrlBean.class);
        viewHolder.tvProductnum.setText("共" + urlBeans.size() + "张图");
        Glide.with(mContext)
                .load(urlBeans.get(0).getUrl())
                .thumbnail(Glide.with(mContext).load(R.drawable.loading))
                .into(viewHolder.cvImage);
        viewHolder.tvLikenum.setText("共" + productListBean.getLikes() + "人喜欢");
        viewHolder.tvTitle.setText(productListBean.getTitle());
        /**
         * 装载横向RecyclerView
         */
        //创建LinearLayoutManager
        LinearLayoutManager linearLayoutManager = new LinearLayoutManager(mContext);
        //RecyclerView设置为横向滑动
        linearLayoutManager.setOrientation(LinearLayoutManager.HORIZONTAL);
        //设置创建LinearLayoutManager到RecyclerView中
        viewHolder.rvLabel.setLayoutManager(linearLayoutManager);
        List extras = new ArrayList();
        if (!productListBean.getLabel().isEmpty()) {
            extras = Arrays.asList(productListBean.getLabel().split(","));
        }
        LabelAdapter labelAdapter = new LabelAdapter(mContext, extras);
        viewHolder.rvLabel.setAdapter(labelAdapter);
        viewHolder.tvFocus.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                tofocus(productListBean.getId_unique());
            }
        });
        viewHolder.llContent.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                toproduct_detail(productListBean.getProduct_id());
            }
        });
        viewHolder.ivHead.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                to_User_home(productListBean.getId_unique());
            }
        });
        return convertView;
    }


    public abstract void tofocus(String id_unique);

    protected abstract void toproduct_detail(String product_id);

    protected abstract void to_User_home(String id_unique);


    static
    class ViewHolder {
        @BindView(R.id.iv_head)
        CircleImageView ivHead;
        @BindView(R.id.tv_name)
        TextView tvName;
        @BindView(R.id.tv_focus)
        TextView tvFocus;
        @BindView(R.id.cv_image)
        ImageView cvImage;
        @BindView(R.id.tv_productnum)
        TextView tvProductnum;
        @BindView(R.id.iv_like)
        ImageView ivLike;
        @BindView(R.id.iv_comment)
        ImageView ivComment;
        @BindView(R.id.iv_collect)
        ImageView ivCollect;
        @BindView(R.id.iv_share)
        ImageView ivShare;
        @BindView(R.id.tv_likenum)
        TextView tvLikenum;
        @BindView(R.id.tv_title)
        TextView tvTitle;
        @BindView(R.id.rv_label)
        RecyclerView rvLabel;
        @BindView(R.id.tv_commentnum)
        TextView tvCommentnum;
        @BindView(R.id.ll_content)
        LinearLayout llContent;

        ViewHolder(View view) {
            ButterKnife.bind(this, view);
        }
    }
}
